package com.neusoft.work.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.work.dao.CustomerCarePlanDao;
import com.neusoft.work.dao.CustomerDao;
import com.neusoft.work.po.Customer;
import com.neusoft.work.po.PageResponseBean;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private CustomerCarePlanDao customerCarePlanDao;

    @Override
    public PageResponseBean<List<Customer>> queryAll(String name,Integer type,Long cur, Long number) {
        // TODO Auto-generated method stub
	    Page<Customer> page = new Page<>(cur, number);
	    QueryWrapper<Customer> qw = new QueryWrapper<>();
	    if(type!=null&&type!=0) {
	    	qw.eq("type", type);
	    }
	    // realName 非空时才添加模糊查询
	    if (name != null && !name.trim().isEmpty()) {
	        qw.like("name", name.trim());
	    }
	    // 逻辑未删除
	    qw.eq("isDeleted", false);

	    // 3. 执行分页查询
	    IPage<Customer> userPage = customerDao.selectPage(page, qw);
	    List<Customer> list = userPage.getRecords();
		long total=userPage.getTotal();
		PageResponseBean<List<Customer>> rb=null;
	    // 4. 返回结果
	    if (total>0) {
	    	rb=new PageResponseBean<>(list);
	    	rb.setTotal(total);
	    } else {
	    	rb=new PageResponseBean<>(500,"no data");
	    }
	    return rb;
	    
    }

    @Override
    public ResponseBean<Integer> addCustomer(Customer customer) {
        // TODO Auto-generated method stub
    	int count = customerDao.insert(customer);
		if (count > 0) {
			return new ResponseBean<>(customer.getId());
		} else {
			return new ResponseBean<>(500, "添加失败");
		}
    }

    @Override
    public ResponseBean<Integer> updateCustomer(Customer customer) {
        // TODO Auto-generated method stub
    	int count = customerDao.updateById(customer);
		if (count > 0) {
			return new ResponseBean<>(customer.getId());
		} else {
			return new ResponseBean<>(500, "更新失败");
		}
    }

    @Override
    public ResponseBean<Integer> deleteCustomer(Integer id) {
        // TODO Auto-generated method stub
    	Customer cu=new Customer();
    	cu.setId(id);
    	cu.setIsDeleted(true);
    	int count = customerDao.updateById(cu);
		if (count > 0) {
			return new ResponseBean<>(count);
		} else {
			return new ResponseBean<>(500, "删除失败");
		}

    }
    
    @Override
    public ResponseBean<List<Customer>> batchByIds(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return new ResponseBean<>(500, "ID 列表不能为空");
        }
        List<Customer> list = customerDao.selectBatchIds(ids);
        return new ResponseBean<>(list);
    }

	@Override
	public ResponseBean<Customer> getById(Integer id) {
		// TODO Auto-generated method stub
		Customer c=customerDao.selectById(id);
		if (c != null) {
            return new ResponseBean<>(c);
        } else {
            return new ResponseBean<>(500, "客户不存在");
        }
	}

}
